// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phonenumber: "",
    password: "",
  },
  /*前往注册*/
  Toregist: function (e) {
    wx.navigateTo({
      url: '/pages/enroll/enroll'
    })
  },
  /*微信登录*/
  wechatlogin: function (e) {
    wx.showModal({
      title: '提示',
      showCancel: false,
      content: '该功能暂未开放，请先注册或者使用账号登录吧^-^',
      success: function (res) { }
    })
  },
  /*登录及信息校验*/
  longin: function (e) {
    var that = this
    if (that.data.phonenumber == '') {
      wx.showModal({
        title: '提示！',
        showCancel: false,
        content: '请输入手机号！',
        success: function (res) { }
      })
    }
    else if (that.data.password == '') {
      wx.showModal({
        title: '提示！',
        showCancel: false,
        content: '请输入密码！',
        success: function (res) { }
      })
    }
    else {
      wx.request({
        url: getApp().globalData.server + '/index.php/Home/User/login',
        data: {
          phone: that.data.phonenumber,
          password: that.data.password,
        },

        method: "POST",
        header: {
          'content-type': "application/x-www-form-urlencoded"
        },
        success(res) {
          if (res.data.error_code == 1) {
            wx.showModal({
              title: '提示！',
              showCancel: false,
              content: res.data.msg,
            })
          } else if (res.data.error_code == 2) {
            wx.showModal({
              title: '提示！',
              showCancel: false,
              content: '该手机号尚未注册',
            })
          } else if (res.data.error_code == 3) {
            wx.showModal({
              title: '提示！',
              showCancel: false,
              content: '密码不正确！',
            })
          } else if (res.data.error_code != 0) {
            wx.showModal({
              title: '抱歉！',
              content: '出现了未知的错误- -',
              showCancel: false,
            })
          } else if (res.data.error_code == 0) {
            getApp().globalData.user = res.data.data
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: '登录成功！',
              complete: function (res) {
                wx.reLaunch({
                  url: "/pages/square/square"
                })
              }
            })
          }
        },
        fail: function (res) {
          wx.showModal({
            title: '提示!',
            content: '网络异常- -',
            showCancel: false,
          })
        }
      })
    }
  },
  phonenumberInput: function (e) {
    this.data.phonenumber = e.detail.value
  },

  passwordInput: function (e) {
    this.data.password = e.detail.value
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})